package com.socialmediaanalytics.client.facebook;

import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.JsArray;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.socialmediaanalytics.client.GWTEntryPoint;
import com.socialmediaanalytics.client.facebook.sdk.FBCore;
import com.socialmediaanalytics.client.facebook.sdk.JSOModel;
import com.socialmediaanalytics.client.interests.ShowMe;
import com.socialmediaanalytics.client.model.User;

public class FacebookFriendLikes {

	public FacebookFriendLikes(FBCore fbCore, final String friendID) {

		this(fbCore, friendID, false);
	}

	public FacebookFriendLikes(FBCore fbCore, final String friendID,
			final boolean me) {

		fbCore.api("/" + friendID + "/likes",
				new AsyncCallback<JavaScriptObject>() {

					@Override
					public void onFailure(Throwable caught) {

					}

					@Override
					public void onSuccess(JavaScriptObject result) {

						JSOModel jso = result.cast();
						JsArray array = jso.getArray("data");

						if (array.length() > 0) {

							User friend = GWTEntryPoint.facebookFriends
									.get(friendID);

							String likes = "";

							int commonLikes = 0;

							for (int i = 0; i < array.length(); i++) {

								JSOModel j = array.get(i).cast();

								String id = j.get("id");

								String name = j.get("name");

								friend.getFacebookLikes().put(id, name);

								if (!me) {

									String commonInterest = GWTEntryPoint.facebookUser
											.getFacebookLikes().get(id);

									if (commonInterest != null) {

										likes += "<a href='http://facebook.com/"
												+ id
												+ "' target='_blank'>"
												+ name + "</a>, ";

										if (GWTEntryPoint.facebookCommonLikes
												.get(id) != null) {

											GWTEntryPoint.facebookCommonLikes
													.put(id,
															GWTEntryPoint.facebookCommonLikes
																	.get(id) + 1);
										} else {

											GWTEntryPoint.facebookCommonLikes
													.put(id, 1);
										}

										commonLikes++;
									}
								}
							}

							String interests = "";
							if (friend.getFacebookInterests() != null) {

								interests += friend.getFacebookInterests();
							}

							if (me) {

								friend.setFacebookInterests(interests
										+ "<br>likes: " + array.length());

							} else {

								if (likes.length() > 0) {

									friend.setFacebookInterests(interests
											+ "<br>friend likes: "
											+ array.length()
											+ "<br>common likes: " + likes);
								}
							}

							GWTEntryPoint.facebookFriends.put(friendID, friend);

							if (!me) {

								if (likes.length() > 0) {

									friend.setFacebookInterests(interests
											+ "<br>friend likes: "
											+ array.length()
											+ "<br>common likes: " + likes);
								}
							}

							if (GWTEntryPoint.facebookCommonFriends
									.get(friendID) != null) {

								GWTEntryPoint.facebookCommonFriends.put(
										friendID,
										GWTEntryPoint.facebookCommonFriends
												.get(friendID) + commonLikes);
							} else {

								GWTEntryPoint.facebookCommonFriends.put(
										friendID, commonLikes);
							}

							if (me) {

								GWTEntryPoint.facebookUser = friend;

								new ShowMe();
							}
						}
					}
				});

	}
}
